//
//  WithdrawViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/23.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "WithdrawViewController.h"

#import "HWZBAPIRequest.h"
#import "Context.h"
#import "NSString+Utils.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "MBProgressHUD+Toast.h"

@interface WithdrawViewController ()

@property (strong, nonatomic) UITextField *bankCardTextField;
@property (strong, nonatomic) UITextField *creditsTextField;
@property (strong, nonatomic) UIButton *withdrawButton;
@property (strong, nonatomic) UILabel *creditsLabel;
@property (strong, nonatomic) UIButton *confirmButton;

@end

@implementation WithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    self.navigationItem.title = @"提现";
    [self setBackNavgationItem];
    
    
    [self createUI];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)createUI {
    UILabel *bankCardLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(0.0f), TRANS_VALUE(0.0f), TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    bankCardLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    bankCardLabel.textColor = I_COLOR_BLACK;
    bankCardLabel.text = @"银行卡";
    bankCardLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:bankCardLabel];
    
    self.bankCardTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(70.0f), TRANS_VALUE(0.0f), TRANS_VALUE(240.0f), TRANS_VALUE(40.0f))];
    self.bankCardTextField.borderStyle = UITextBorderStyleNone;
    self.bankCardTextField.backgroundColor = I_COLOR_WHITE;
    self.bankCardTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.bankCardTextField.textColor = I_COLOR_BLACK;
    self.bankCardTextField.textAlignment = NSTextAlignmentLeft;
    self.bankCardTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写银行卡号" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.bankCardTextField.tintColor = I_COLOR_BLACK;
    self.bankCardTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.bankCardTextField];
    self.bankCardTextField.enabled = NO;
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f), SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView1];
    
    UILabel *creditsLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(0.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    creditsLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    creditsLabel.textColor = I_COLOR_BLACK;
    creditsLabel.text = @"￥";
    creditsLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:creditsLabel];
    
    self.creditsTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(70.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(150.0f), TRANS_VALUE(40.0f))];
    self.creditsTextField.borderStyle = UITextBorderStyleNone;
    self.creditsTextField.backgroundColor = I_COLOR_WHITE;
    self.creditsTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.creditsTextField.textColor = I_COLOR_BLACK;
    self.creditsTextField.textAlignment = NSTextAlignmentLeft;
    self.creditsTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写提现金额" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.creditsTextField.tintColor = I_COLOR_BLACK;
    [self.view addSubview:self.creditsTextField];
    self.creditsTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView2 = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(219.0f), TRANS_VALUE(40.0f) + 1.0f + TRANS_VALUE(5.0f) , TRANS_VALUE(1.0f), TRANS_VALUE(30.0f))];
    dividerView2.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView2];
    
    self.withdrawButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(220.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(100.0f), TRANS_VALUE(40.0f))];
    self.withdrawButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
    [self.withdrawButton setTitleColor:I_COLOR_RED forState:UIControlStateNormal];
    [self.withdrawButton setTitle:@"全部提现" forState:UIControlStateNormal];
    [self.view addSubview:self.withdrawButton];
    [self.withdrawButton addTarget:self action:@selector(withdrawButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *dividerView3 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(80.0f) + 2.0f, SCREEN_WIDTH, 1.0f)];
    dividerView3.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView3];
    
    self.creditsLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(20.0f), TRANS_VALUE(80.0f) + 2.0f, TRANS_VALUE(280.0f), TRANS_VALUE(40.0f))];
    self.creditsLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(11.0f)];
    self.creditsLabel.textColor = I_COLOR_BLACK;
    self.creditsLabel.numberOfLines = 0;
    self.creditsLabel.text = @"有0可提现\n(最大可提现额为账户余额扣除5%后100的整数倍)";
    self.creditsLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.creditsLabel];

    
    self.confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(25.0f), TRANS_VALUE(130.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(25.0f), TRANS_VALUE(36.0f))];
    self.confirmButton.clipsToBounds = YES;
    self.confirmButton.layer.borderColor = I_COLOR_RED.CGColor;
    self.confirmButton.layer.borderWidth = 1.0f;
    self.confirmButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.confirmButton setTitleColor:I_COLOR_RED forState:UIControlStateNormal];
    [self.confirmButton setTitle:@"确认提现" forState:UIControlStateNormal];
    [self.view addSubview:self.confirmButton];
    
    [self.confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)loadData {
    if([Context sharedInstance].accountInfo) {
        NSString *goldenGredits = [Context sharedInstance].accountInfo.goldenAccount;
        goldenGredits = goldenGredits != nil ? goldenGredits : @"0";
        double tempGredits = [goldenGredits integerValue] / 1.05f;
        NSInteger availableGredits = (long)tempGredits - ((long)tempGredits % 100);
        self.creditsLabel.text = [NSString stringWithFormat:@"您的账户余额为%@, 最多可提现%ld\n(最大可提现额为账户余额扣除5%%后100的整数倍)", goldenGredits, availableGredits];
    }
    if([Context sharedInstance].userInfo) {
        NSString *bankCard = [Context sharedInstance].userInfo.bankCard;
        self.bankCardTextField.text = bankCard;
    }
}

#pragma mark - UIButton Aciton
//全部体现
- (void)withdrawButtonAction:(id)sender {
    self.editing = NO;
    NSString *goldenGredits = [Context sharedInstance].accountInfo.goldenAccount;
    goldenGredits = goldenGredits != nil ? goldenGredits : @"0";
    double tempGredits = [goldenGredits integerValue] / 1.05f;
    NSInteger availableGredits = (long)tempGredits - ((long)tempGredits % 100);
    self.creditsTextField.text = [NSString stringWithFormat:@"%ld", availableGredits];
    if([goldenGredits integerValue] <= 0) {
        [MBProgressHUD showToast:@"您的可提现数为0" initWithView:self.view];
        return;
    }
}

//确定提现
- (void)confirmButtonAction:(id)sender {
    [self.creditsTextField resignFirstResponder];
    NSString *goldenGredits = [Context sharedInstance].accountInfo.goldenAccount;
    goldenGredits = goldenGredits != nil ? goldenGredits : @"0";
    double tempGredits = [goldenGredits integerValue] / 1.05f;
    NSInteger availableGredits = (long)tempGredits - ((long)tempGredits % 100);
    NSString *money = self.creditsTextField.text;
    if([NSString isBlankString:money]) {
        [MBProgressHUD showToast:@"请输入提现金额" initWithView:self.view];
        return;
    } else if([money integerValue] <= 0) {
        [MBProgressHUD showToast:@"请输入大于零的提现金额" initWithView:self.view];
        return;
    } else if([money integerValue] % 100 != 0) {
        [MBProgressHUD showToast:@"请输入100的整数倍金额进行提现" initWithView:self.view];
        return;
    } else if([money integerValue] > availableGredits) {
        [MBProgressHUD showToast:@"您的输入值超过最大可提现额度" initWithView:self.view];
        return;
    }
    
    [self withdrawWithMoney:money];
}

- (void)withdrawWithMoney:(NSString *)money {
    NSString *bankCard = self.bankCardTextField.text;
    if([NSString isBlankString:bankCard]) {
        [MBProgressHUD showToast:@"请输入银行卡号" initWithView:self.view];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
//    [params setObject:bankCard forKey:@"bankCard"];
    [params setObject:money forKey:@"money"];
    [SVProgressHUD showWithStatus:@"正在提交提现申请, 请稍等..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] withdrawWithParams:params success:^(CommonInfo *resultInfo) {
        [SVProgressHUD dismiss];
        if(resultInfo != nil && [resultInfo isSuccess]) {
            [MBProgressHUD showToast:resultInfo.msg initWithView:self.view];
            [self refreshUIWithCredits:money];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAccountInfoChange object:nil];
        } else {
            [MBProgressHUD showToast:@"提现失败" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

- (void)refreshUIWithCredits:(NSString *)credits {
    NSString *goldenGredits = [Context sharedInstance].accountInfo.goldenAccount;
    goldenGredits = goldenGredits != nil ? goldenGredits : @"0";
    NSInteger result = [goldenGredits integerValue] - [credits integerValue];
    double tempGredits = result / 1.05f;
    NSInteger availableGredits = (long)tempGredits - ((long)tempGredits % 100);
    self.creditsLabel.text = [NSString stringWithFormat:@"您的账户余额为%ld, 最多可提现%ld(最大可提现额为账户余额扣除5%%后100的整数倍)", result, availableGredits];
    [Context sharedInstance].accountInfo.goldenAccount = [NSString stringWithFormat:@"%ld", result];
}

@end
